// pages/order/order.js
import {
    request
} from '../../util/request'
import {
    createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
    store
} from '../../store/store'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        Product_list: [], //商品列表
        payment: '',
        Actual: 0,
        amount: 0, //实付
        information: {
            place: "秦琪网咖（淮海中路）二楼包间309",
            name: "张女士",
            phone: 17754222111
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: ['Selected_product', 'Total'],
            actions: ['Selected_product_List', 'TotalList']
        })

        this.setData({
            Product_list: store.Selected_product.filter(function (item) {
                return item != false
            }),
            amount: store.Total
        })
        console.log('商品', this.data.Product_list);
    },

    selectAction(ev) {

        this.setData({
            payment: ev.detail.value
        })
    },

    // 提交订单
    btnAction(ev) {
        console.log(this.data.Product_list);
        console.log(this.data.amount);
        console.log(...this.data.payment);
        console.log(this.data.information);
        console.log(wx.getStorageSync("token"))
        request({
            url: 'http://localhost:8000/api/wechat/all',
            method: 'get'
        }).then(res => {
            console.log(res.data.data.openid);
            request({
                url: 'http://localhost:8000/api/orders/add',
                method: 'post',
                data: {
                    useid: res.data.data.openid,
                    key: parseInt(Math.random() * 1000000),
                    time: new Date().getTime(),
                    paymentMethod: this.data.payment[0],
                    productAmount: this.data.amount,
                    payAmount: this.data.amount,
                    productQuantity: 2,
                    state: 2,
                    AddressId: JSON.stringify(this.data.information),
                    orderItem: JSON.stringify(this.data.Product_list),
                }
            }).then(res => {
                console.log(res)
                wx.showToast({
                    title: '订单提交成功',
                    icon: 'success',
                    duration: 2000
                })
                const aa = setTimeout(function () {
                    wx.switchTab({
                    url: `/pages/classify/classify`,
                    success: (res) => {
                        console.log(res);
                    },
                    fail: (res) => {
                        console.log(res);
                    }
                })
                clearTimeout(aa)
                },2000)
                
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.storeBindings.destroyStoreBindings();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})